/**
 * 
 */
package switchviz.graphic;

import java.awt.event.ComponentEvent;
import java.awt.event.ComponentListener;

import javax.media.opengl.GLJPanel;

import com.sun.opengl.util.FPSAnimator;


/**
 * @author Thomas
 *
 */
public class GraphicsPanel extends GLJPanel implements ComponentListener
{
	private static final long serialVersionUID = 1L;
	private FPSAnimator _animator = null;
	//private static Logger logger = Logger.getLogger(GraphicsPanel.class);
	
	private GraphicEventListener _eventListener = null;
	
	public GraphicsPanel(GraphicEventListener eventListener)
	{
		super();
		
		initialize(eventListener);
	}
	
	private void initialize(GraphicEventListener eventListener)
	{
		setEventListener(eventListener);
		addGLEventListener(getEventListener());
		this.addComponentListener(this);
		this.addMouseListener(getEventListener());
		this.addMouseMotionListener(getEventListener());
		this.addMouseWheelListener(getEventListener());
		_animator = new FPSAnimator(this, 60);
		
		_animator.start();
	}
	
	private void setEventListener(GraphicEventListener eventListener)
	{
		_eventListener = eventListener;
	}
	
	private GraphicEventListener getEventListener()
	{
		return _eventListener;
	}

	public void componentHidden(ComponentEvent arg0) 
	{
		if(_animator.isAnimating())
			_animator.stop();
	}
	

	public void componentShown(ComponentEvent arg0) 
	{
		if(!_animator.isAnimating())
			_animator.start();
	}
	
	public void componentMoved(ComponentEvent arg0) {}
	public void componentResized(ComponentEvent arg0) {}
}
